package com.colorful.insuranceproject.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.NetWorkImageHolderView;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.CityListBean;
import com.colorful.insuranceproject.claims.PhysicalExaminationAppointmentActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.consts.URLConst;
import com.colorful.insuranceproject.home.MessageActivity;
import com.colorful.insuranceproject.microDrug.DrugAddressActivity;
import com.colorful.insuranceproject.request.HomeRequest;
import com.lovely3x.common.beans.LocationWrapper;
import com.lovely3x.common.fragments.BaseCommonFragment;
import com.lovely3x.common.managements.LocationManager;
import com.lovely3x.common.utils.Response;
import com.lovely3x.common.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/18.
 * 服务板块
 */
public class ServerFragment extends BaseCommonFragment implements OnItemClickListener {

    private static final int GET_SERVER_PAGE_DATA = 0X1;

    private static final int GET_NOW_POSITION = 0X2;

    HomeRequest homeRequest;

    @Bind(R.id.cb_banner)
    ConvenientBanner banner;

    List<String> mBannerUrls;

    private int type;

    @Override
    protected int getContentView() {
        return R.layout.fragment_claims;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this, mRootView);
    }

    @Override
    protected void onViewInitialized() {
        initBanner();
        homeRequest = new HomeRequest(getHandler());
        homeRequest.getServerPageBanner(GET_SERVER_PAGE_DATA);
    }

    private void initBanner() {
        // 设置记录的小标点
        banner.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
        banner.setPageIndicator(new int[]{R.drawable.banner_page_indicator_ponit_uncheck, R.drawable.banner_page_indicator_point_checked});
        banner.setOnItemClickListener(this);
        banner.startTurning(5000);//5秒一翻页
        banner.setCanLoop(true);//无限循环
        ViewGroup.LayoutParams params = banner.getLayoutParams();
        params.width = ViewUtils.getWidth();
        params.height = (int) (params.width * (2 * 1.0f / 5));
        banner.setLayoutParams(params);

        mBannerUrls = new ArrayList<>();
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_SERVER_PAGE_DATA:
                if (response.isSuccessful) {
                    mBannerUrls = (List<String>) response.obj;
                    if (mBannerUrls.size() <= 1) {
                        banner.setCanLoop(false);//无限循环
                        banner.setPointViewVisible(false);
                    }
                    banner.setPages(new CBViewHolderCreator<Holder<String>>() {
                        @Override
                        public Holder<String> createHolder() {
                            return new NetWorkImageHolderView();
                        }
                    }, mBannerUrls);
                } else {
                    mActivity.showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;

            case GET_NOW_POSITION:
                LocationManager.getInstance().stop();
                if (response.isSuccessful) {
                    CityListBean cb = (CityListBean) response.obj;
                    if (type == 1) {
                        mActivity.launchActivity(PhysicalExaminationAppointmentActivity.class, PhysicalExaminationAppointmentActivity.EXTRA_POSITION, cb);
                    } else {
                        mActivity.launchActivity(DrugAddressActivity.class, DrugAddressActivity.EXTRA_POSITION, cb);
                    }
                } else {
                    mActivity.showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    protected void restoreInstanceOnCreateBefore(Bundle savedInstanceState) {

    }

    @Override
    protected void restoreInstanceOnCreateAfter(Bundle savedInstanceState) {

    }

    /**
     * 页面列表监听
     */
    @OnClick({R.id.rl_cosmetic_dentistry, R.id.rl_medical_institution, R.id.rl_Regular_registration,
            R.id.rl_Physical_examination_appointment, R.id.rl_Nearby_drugstore, R.id.img_guest_service, R.id.img_message})
    void OnClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_cosmetic_dentistry://牙科美容
                break;
            case R.id.rl_medical_institution://医疗机构
                break;
            case R.id.rl_Regular_registration://普通挂号
                break;
            case R.id.rl_Physical_examination_appointment://体检预约
                getPosition(1);
                break;
            case R.id.rl_Nearby_drugstore://附近的药店
                getPosition(2);
                break;
            case R.id.img_guest_service:// 客服
                final String number = URLConst.CUSTOMER_SERVICE_TELEPHONE_NUMBERS;
                mActivity.showConfirmDialog(getString(R.string.contact_customer_service), number, null, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + getString(R.string.call_constomer_server)));
                        startActivity(intent);
                    }
                }).show();
                break;
            case R.id.img_message: // 消息
                mActivity.launchActivity(MessageActivity.class);
                break;
        }
    }

    @Override
    public void onItemClick(int position) {
        mActivity.showHTTPURLImgOnNewActivity(mBannerUrls);
    }

    private void getPosition(int id) {
        LocationWrapper loc = LocationManager.getInstance().getCurrentLocation();
        if (loc == null) loc = new LocationWrapper();
        type = id;
//        mActivity.showToast("精度" + loc.getLatitude() + "维度" + loc.getLongitude());
        homeRequest.getCurrentAddr(loc.getLatitude(), loc.getLongitude(), GET_NOW_POSITION);
    }

    @Override
    public void onUserHintVisibleChanged(boolean isVisibleToUser, boolean isFirstVisible) {
        super.onUserHintVisibleChanged(isVisibleToUser, isFirstVisible);
        if (isVisibleToUser) {
            LocationManager.getInstance().start();
        } else {
            LocationManager.getInstance().stop();
        }
    }
}